//
//  WallpaperDownloadView.m
//  Pictorial
//
//  Created by SKY on 15/12/29.
//  Copyright © 2015年 sky. All rights reserved.
//

#import "WallpaperDownloadView.h"
#import "DownloadModel.h"
#import "DownloadData.h"
#import <UIImageView+WebCache.h>

#define ALERT_INTERVAL 100

@interface WallpaperDownloadView ()
@property (weak, nonatomic) IBOutlet UIImageView *picture;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UIView *detailView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;

@end
@implementation WallpaperDownloadView

-(void)setPictureString:(NSString *)pictureString{
    _pictureString = pictureString;
    [self.picture imageLoadProgressWithNetImage:pictureString];
}
-(void)willMoveToSuperview:(UIView *)newSuperview{
    
    self.frame = newSuperview.bounds;
    
    self.detailView.layer.cornerRadius = 5;
    self.detailView.clipsToBounds = YES;
    self.viewHeight.constant = 0;
    [self.detailView layoutIfNeeded];
    
    self.picture.userInteractionEnabled = YES;
    if (self.removeSuperView == YES) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeSuper)];
        [self.picture addGestureRecognizer:tap];
    }else{
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(back)];
        [self.picture addGestureRecognizer:tap];
    }
}
-(void)removeSuper{
    [self.superview removeFromSuperview];
}
-(void)back{
    [self removeFromSuperview];
}
- (IBAction)more:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    
    if (sender.isSelected) {
        
        self.titleLabel.text = self.title;
        self.descLabel.text = self.desc;
        NSString *str = self.title.length > self.desc.length ? (self.title) : (self.desc);
        CGFloat height = [str heightWithMaxWidth:15 attributes:@{NSFontAttributeName:Font(15)}];
        if (height > 350) {
            height = 350;
        }
        
        [UIView animateWithDuration:0.5 animations:^{
            [sender setTransform:CGAffineTransformMakeRotation(M_PI_2 / 90 * 135)];
            
            self.viewHeight.constant = height + 30;
            [self.detailView layoutIfNeeded];
            [self.detailView setNeedsLayout];
        }];
    }else{
        self.titleLabel.text = nil;
        self.descLabel.text = nil;
        
        [UIView animateWithDuration:0.5 animations:^{
            [sender setTransform:CGAffineTransformMakeRotation(0)];
            self.viewHeight.constant = 0;
            [self.detailView layoutIfNeeded];
            [self.detailView setNeedsLayout];
        }];
    }
}
- (IBAction)download:(id)sender {
    
    DownloadModel *model = [DownloadModel classInitializtion];
    model.title = self.title;
    model.picture = self.pictureString;
    model.desc = self.desc;
    
    NSArray *array = [DownloadData downloadData];
    for (int index = 0; index < array.count; index ++) {
        NSData *data = array[index];
        DownloadModel *dataModel = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        if ([self.pictureString isEqualToString:dataModel.picture]) {
            [self alertDownload:@"~已经保存~"];
            return;
        }
    }
    [DownloadData addPicture:model];

    //保存图片到相册
    
    UIImage *photo = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.pictureString]]];
    
    UIImageWriteToSavedPhotosAlbum(photo, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (!error) {
        [self alertDownload:@"~保存成功~"];
    }else
    {
        [self alertDownload:@"~保存失败~"];
    }
}
-(void)alertDownload:(NSString *)str{
    
    UILabel *alert = [UILabel labelWithText:str fontSize:15 frame:CGRectMake(ALERT_INTERVAL, HEIGHT - 140, WIDTH - ALERT_INTERVAL * 2, 40) color:[UIColor whiteColor] textAlignment:1];
    [self addSubview:alert];
    alert.alpha = 0.8;
    alert.backgroundColor = [UIColor blackColor];
    
    [UIView animateWithDuration:1.0 animations:^{
        alert.alpha = 0;
    } completion:^(BOOL finished) {
        [alert removeFromSuperview];
    }];
}

@end
